//
//  SISSUploadImageShowView.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/3.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSUploadImageShowView.h"
#import "SISSUploadImageCollectionViewCell.h"
#import "SISSPhotoBrowser.h"

@interface SISSUploadImageShowView ()<UICollectionViewDelegate,UICollectionViewDataSource,MWPhotoBrowserDelegate>

@property (weak, nonatomic) UILabel *titlelabel;
@property (weak, nonatomic) UILabel *imgNumlabel;
@property (weak, nonatomic) UICollectionView *collectionView;

@end
@implementation SISSUploadImageShowView

- (void)setTitleStr:(NSString *)titleStr {
    _titleStr = titleStr;
    self.titlelabel.text = titleStr;
    
}

- (void)setImageUrlArr:(NSMutableArray *)imageUrlArr {
    _imageUrlArr = imageUrlArr;
    [self updateCollectionViewConstraint];
}

- (void)setImages:(NSMutableArray *)images {
    _images = images;
    [self updateCollectionViewConstraint];
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.imageUrlArr.count > 0) {
        return self.imageUrlArr.count > self.maxShow ? self.maxShow : self.imageUrlArr.count;
    }
    return self.images.count > self.maxShow ? self.maxShow : self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSUploadImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:uploadImageCollectionViewCellReuseID forIndexPath:indexPath];
    
    if (self.images.count > 0) {
        cell.image = self.images[indexPath.item];
    } else {
        cell.imageUrl = self.imageUrlArr[indexPath.item];
    }
    cell.isShowDeleteButton = NO;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    MWPhotoBrowser *photoBrowser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    [photoBrowser setCurrentPhotoIndex:indexPath.item];
    //    [photoBrowser showNextPhotoAnimated:YES];
    //    [photoBrowser showPreviousPhotoAnimated:YES];
    SISSNavigationController *navigationController = [[SISSNavigationController alloc] initWithRootViewController:photoBrowser];
    navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
    [[self getCurrentViewController] presentViewController:navigationController animated:YES completion:nil];
    
}


#pragma mark -- MWPhotoBrowserDelegate
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.imageUrlArr.count > 0 ? self.imageUrlArr.count : self.images.count;
}

- (id<MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    
    //创建图片模型
    //    MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:self.images[index]]];
    MWPhoto *photo;
    if (self.images.count > 0) {
        photo = [MWPhoto photoWithImage:self.images[index]];
    } else {
        photo = [MWPhoto photoWithURL:[NSURL URLWithString:self.imageUrlArr[index]]];
    }
    
    return photo;
}

#pragma mark - 更新collectionView约束
- (void)updateCollectionViewConstraint {
    NSInteger count = self.imageUrlArr.count > 0 ? self.imageUrlArr.count : self.images.count;
    self.imgNumlabel.text = [NSString stringWithFormat:@"%zd/%zd",count,self.maxShow];
    CGFloat height = count < 4 ? SCREEN_WIDTH / 3 : (count < 7 ? 2 * SCREEN_WIDTH / 3 : SCREEN_WIDTH);
    
    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(height);
    }];
    [self.collectionView reloadData];
}

#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    self.backgroundColor = [UIColor whiteColor];
    
    self.maxShow = 9;
    
    UILabel *titlelabel = [UILabel yyr_labelWithText:@"合同信息" fontSize:15 textColor:MAIN_TEXT_COLOR];
    [self addSubview:titlelabel];
    self.titlelabel = titlelabel;
    
    UILabel *imgNumlabel = [UILabel yyr_labelWithText:[NSString stringWithFormat:@"0/%zd",self.maxShow] fontSize:15 textColor:SECONDARY_TEXT_COLOR];
    imgNumlabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:imgNumlabel];
    self.imgNumlabel = imgNumlabel;
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 10;
    flowLayout.minimumInteritemSpacing = 10;
    CGFloat itemW = (SCREEN_WIDTH - 3 * 20) / 3;
    flowLayout.itemSize = CGSizeMake(itemW, itemW);
    //    flowLayout.itemSize = CGSizeMake(65, 65);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [collectionView registerClass:[SISSUploadImageCollectionViewCell class] forCellWithReuseIdentifier:uploadImageCollectionViewCellReuseID];
    [self addSubview:collectionView];
    self.collectionView = collectionView;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    [self.titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(weakSelf).offset(2 * globelMargin);
    }];
    
    [self.imgNumlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.titlelabel);
        make.right.equalTo(weakSelf.mas_right).offset(-3 * globelMargin);
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.titlelabel.mas_bottom).offset(2 * globelMargin);
        make.left.right.equalTo(weakSelf);
        make.height.mas_equalTo(SCREEN_WIDTH / 3);
        make.bottom.equalTo(weakSelf).offset(-2 * globelMargin);
    }];
    
}


@end
